import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/base/webview/base.dart';
import 'package:flutter_ahj_app/base/webview/webview_scaffold.dart';
import 'package:path_provider/path_provider.dart';

import 'mTopBar.dart';

///webview
class MineWebView extends StatefulWidget {
  // 标题，地址
  String _title, _url;
  bool pdf;
  bool zoom;

  MineWebView(this._title, this._url, {Key key, this.pdf, this.zoom = false})
      : assert(_title != null, _url != null);

  @override
  MineWebViewState createState() => MineWebViewState();
}

class MineWebViewState extends State<MineWebView> {
  FlutterWebviewPlugin flutterWebviewPlugin = new FlutterWebviewPlugin();

  @override
  void initState() {
    super.initState();

    flutterWebviewPlugin.getDownLoadFileData.listen((data) {
      int time = DateTime.now().millisecondsSinceEpoch;
      if (time - mCurrentTime < 300) {
        return;
      }
      mCurrentTime = time;


    });



    flutterWebviewPlugin.getLocation.listen((data) {
      int time = DateTime.now().millisecondsSinceEpoch;
      if (time - mCurrentTime < 300) {
        return;
      }
      mCurrentTime = time;

    });
    flutterWebviewPlugin.onDestroy.listen((data) {
      // 关闭页面
      int time = DateTime.now().millisecondsSinceEpoch;
      if (time - mCurrentTime < 300) {
        return;
      }
      mCurrentTime = time;
      if (Platform.isAndroid) {
        _back();
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: WebviewScaffold(
        appBar: MTopBar(
            title: widget._title,
            leftButtonIcon: Icons.arrow_back,
            onLeftButtonIconClick: () => _back()).create(),
        url: widget._url,
        pdf: widget.pdf,
        withZoom: widget.zoom,
      ),
      onWillPop: () => _back(),
    );
  }

  _back() {
    Navigator.pop(context);
  }

  int mCurrentTime = 0; // 当前时间

}
